/**
 * Author:yuiop
 * Date:2022/7/4 16:06
 */
package com.devops.admin.controller;

import com.devops.admin.service.DOSK8sDeploymentService;
import com.devops.admin.service.DOSProjectVersionService;
import com.devops.admin.service.DOSReleaseService;
import com.devops.common.utils.Message;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


/**
 * @author heyabo
 */
@RestController
@RequestMapping("/release")
@Api(value = "/release",tags = "Release相关接口")
public class ReleaseController {
    @Autowired
    DOSK8sDeploymentService dosk8sDeploymentService;
    @Autowired
    DOSReleaseService dosReleaseService;
    @Autowired
    DOSProjectVersionService dosProjectVersionService;

    @GetMapping("/restart/{releaseName}")
    @ApiOperation(value="release重启(name)",notes = "根据releaseName重启release")
    @ApiImplicitParams({@ApiImplicitParam(name = "releaseName",value = "releaseName",required = true,dataType = "String",paramType = "path")})
    public Message restartReleaseByName(@PathVariable("releaseName") String releaseName) throws IOException {
        if (dosk8sDeploymentService.restartReleaseByName(releaseName)) {
            return Message.ok("重启成功");
        } else {
            return Message.error("重启失败");
        }
    }

    @GetMapping("/restart/id/{releaseId}")
    @ApiOperation(value="release重启(id)",notes = "根据releaseId重启release")
    @ApiImplicitParams({@ApiImplicitParam(name = "releaseId",value = "releaseId",required = true,dataType = "Integer",paramType = "path")})
    public Message restartReleaseByName(@PathVariable("releaseId") Integer releaseId) throws IOException {
        dosReleaseService.checkRestartPermission(releaseId);
        if (dosk8sDeploymentService.restartRelease(releaseId)) {
            return Message.ok("重启成功");
        } else {
            return Message.error("重启失败");
        }
    }
}
